package com.gmss.crawler.module.resume.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gmss.crawler.module.person.domain.PersonInfo;
import com.gmss.crawler.module.resume.domain.Resume;
import com.gmss.crawler.module.resume.mapper.ResumeMapper;
import com.gmss.crawler.module.resume.service.IResumeService;
import com.gmss.crawler.utils.CommonUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 个人履历表 服务实现类
 * </p>
 *
 * @author Auto-generator
 * @since 2021-10-19
 */
@Service
public class ResumeServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements IResumeService {

    @Override
    public void saveResume(PersonInfo personInfo, List<Resume> list) {

        for (Resume resu : list) {
            resu.setPersonId(personInfo.getId());
        }
        LambdaQueryWrapper<Resume> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Resume::getPersonId, personInfo.getId());
        int count = count(queryWrapper);
        if (count > 0) {
            remove(queryWrapper);
        }
        saveBatch(list);
    }

    @Override
    public int queryCount(PersonInfo personInfo, Resume resumeData) {
        LambdaQueryWrapper<Resume> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Resume::getPersonId, personInfo.getId());

        if (null != resumeData.getFormat() && !resumeData.getFormat()) {
            String[] datas = resumeData.getResume().split(" ");
            for (int i = 0; i < datas.length; i++) {
                queryWrapper.like(Resume::getResume, datas[i]);
            }
//            queryWrapper.eq(Resume::getResumeDate, resumeData.getResumeDate());
        } else {
            queryWrapper.like(Resume::getResume, resumeData.getResume());
        }
        return count(queryWrapper);
    }

    @Override
    public void queryResumeSave(PersonInfo personInfo, String data) {
        Resume resume = new Resume().builder().personId(personInfo.getId()).resume(data).build();
        LambdaQueryWrapper<Resume> wrapper = new LambdaQueryWrapper();
        wrapper.eq(Resume::getPersonId, personInfo.getId());
//        wrapper.eq(Resume::getResume, data);
//        List<Resume> resumeList = list(wrapper);
//        if (CollectionUtils.isEmpty(resumeList))
        if (count(wrapper) <= 0) {
            save(resume);
        }
    }

    /**
     * 查询personId在Resume（人物经历）表中是否有数据，如果没有插入，有则不处理。
     *
     * @param resumeList
     * @param personInfo
     */
    @Override
    public void saveResumeByPersonId(PersonInfo personInfo, List<Resume> resumeList) {

        if (CollectionUtils.isNotEmpty(resumeList)) {
            LambdaQueryWrapper<Resume> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Resume::getPersonId, personInfo.getId());
            int count = count(queryWrapper);
            if (count <= 0) {
                for (Resume queryResumeData : resumeList) {
                    queryResumeData.setPersonId(personInfo.getId());
                    if (queryResumeData.getFormat() != null && !queryResumeData.getFormat()) {
                        save(queryResumeData);
                    } else {
                        queryResumeData.setResume(queryResumeData.getResumeDate());
                        queryResumeData.setResumeDate(null);
                        save(queryResumeData);
                    }

                }
            }
        }
    }
}
